package com.laker.autotest.desktop.dev;

import com.laker.autotest.desktop.utils.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.event.InputEvent;
import java.util.List;

@Slf4j
public class RobotPlaybackStrategy implements PlaybackStrategy {
    @Override
    public void replay(List<String> actions) throws Exception {
        Robot robot = new Robot();
        int lastX = -1, lastY = -1;
        int actionIndex = 0;
        for (String action : actions) {
            log.info("[Replay] Action {}: {}", actionIndex++, action);
            String[] s = action.split("#");
            final long delay = Long.parseLong(s[0]);
            long safeDelay = Math.min(delay, 5000); // 最多sleep 5秒
            if (delay > 5000) log.info("[Replay] Delay {} ms capped to 5000 ms", delay);
            int type = Integer.parseInt(s[1]);
            log.info("[Replay] Sleep {} ms before action", safeDelay);
            Thread.sleep(safeDelay);
            if (type == ObjectUtil.NATIVE_MOUSE_EVENT) {
                int id = Integer.parseInt(s[2]);
                int modifiers = Integer.parseInt(s[3]);
                int x = Integer.parseInt(s[4]);
                int y = Integer.parseInt(s[5]);
                int button = Integer.parseInt(s[6]);
                log.info("[Replay] MouseEvent id={}, x={}, y={}, button={}", id, x, y, button);
                if (id == java.awt.event.MouseEvent.MOUSE_MOVED || id == java.awt.event.MouseEvent.MOUSE_DRAGGED || id == 2503) {
                    if (x != lastX || y != lastY) {
                        log.info("[Replay] mouseMove({}, {})", x, y);
                        robot.mouseMove(x, y);
                        lastX = x;
                        lastY = y;
                    }
                }
                if (id == java.awt.event.MouseEvent.MOUSE_PRESSED || id == 2501) {
                    if (button > 0) {
                        log.info("[Replay] mousePress({})", button);
                        robot.mousePress(getAwtButtonMask(button));
                    }
                }
                if (id == java.awt.event.MouseEvent.MOUSE_RELEASED || id == 2502) {
                    if (button > 0) {
                        log.info("[Replay] mouseRelease({})", button);
                        robot.mouseRelease(getAwtButtonMask(button));
                    }
                }
            } else if (type == ObjectUtil.NATIVE_KEY_EVENT) {
                int id = Integer.parseInt(s[2]);
                int modifiers = Integer.parseInt(s[3]);
                int rawCode = Integer.parseInt(s[4]);
                int keyCode = Integer.parseInt(s[5]);
                char keyChar = s[6].charAt(0);
                int keyLocation = Integer.parseInt(s[7]);
                log.info("[Replay] KeyEvent id={}, keyCode={}, keyChar={}", id, keyCode, keyChar);
                if (id == java.awt.event.KeyEvent.KEY_PRESSED) {
                    log.info("[Replay] keyPress({})", keyCode);
                    robot.keyPress(keyCode);
                } else if (id == java.awt.event.KeyEvent.KEY_RELEASED) {
                    log.info("[Replay] keyRelease({})", keyCode);
                    robot.keyRelease(keyCode);
                }
            } else if (type == ObjectUtil.NATIVE_MOUSEWHEEL_EVENT) {
                int wheelRotation = Integer.parseInt(s[9]);
                log.info("[Replay] mouseWheel({})", wheelRotation);
                robot.mouseWheel(wheelRotation);
            }
        }
        log.info("Replay completed (macOS Robot mode).");
    }

    private int getAwtButtonMask(int button) {
        switch (button) {
            case 1:
                return InputEvent.BUTTON1_DOWN_MASK;
            case 2:
                return InputEvent.BUTTON2_DOWN_MASK;
            case 3:
                return InputEvent.BUTTON3_DOWN_MASK;
            default:
                return InputEvent.BUTTON1_DOWN_MASK;
        }
    }
}